package cn.hd01.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.hd01.repository.entity.HotRecommend;
import cn.hd01.service.HotRecommendService;
import cn.hd01.service.UploadService;
import cn.hd01.web.auth.Auth;
import cn.hd01.web.util.WebException;
import cn.hd01.weixin.model.Result;

@Controller
@RequestMapping("/hotRecommend")
@Auth
public class HotRecommendController {
	private Logger logger = LoggerFactory.getLogger(HotRecommendController.class);
	
	@Autowired
	private HotRecommendService hotRecommendService;
	@Autowired
	private UploadService uploadService;
	@Value("${upload.dir}")
	private String uploadDir;
	
	@RequestMapping(method = RequestMethod.GET)
	public String index(Model m, HttpServletRequest request) {
		return "hotrecommend";
	}

	@ResponseBody
	@RequestMapping(path = "/list", method = RequestMethod.GET)
	private Page<HotRecommend> find(HotRecommend s, @PageableDefault Pageable pageable) {
		Page<HotRecommend> pageProduct = hotRecommendService.findAll(s, pageable);
		return pageProduct;
	}

	@ResponseBody
	@RequestMapping(path = "/delete/{id}", method = RequestMethod.GET)
	private void delete(@PathVariable("id") Integer id) {
		hotRecommendService.delete(id);
	}

	@ResponseBody
	@RequestMapping(path = "/save", method = RequestMethod.POST)
	private void save(HotRecommend hotRecommend) {

		validateHotRecommend(hotRecommend);
		
		if(hotRecommend.getId() == null){//保存
			
			Date date = new Date();
			
			hotRecommend.setCreateTime(date);
			hotRecommend.setUpdateTime(date);
			hotRecommendService.save(hotRecommend);
			
		}else {//更新
			hotRecommendService.updateHotRecommend(hotRecommend.getName(), hotRecommend.getUrl(), hotRecommend.getImage(), hotRecommend.getId());
		}
	}

	private void validateHotRecommend(HotRecommend hotRecommend) {

		if (StringUtils.isEmpty(hotRecommend.getName())) {
			throw new WebException(HttpStatus.BAD_REQUEST, "名字不能为空");
		}
		if (StringUtils.isEmpty(hotRecommend.getUrl())) {
			throw new WebException(HttpStatus.BAD_REQUEST, "链接不能为空");
		}
		if (StringUtils.isEmpty(hotRecommend.getImage())) {
			throw new WebException(HttpStatus.BAD_REQUEST, "图片不能为空");
		}
	}
	
	@ResponseBody
	@RequestMapping(path = "/upload", method = RequestMethod.POST)
	private Result upload(@RequestParam("myfile") MultipartFile file, HttpServletRequest request){
		Result result  = new Result();
		String imagePath = null;
		try {
			String basepath = request.getSession().getServletContext().getRealPath("/upload/hotrecommend");
			File f = uploadService.getFile(file.getOriginalFilename(),basepath);
			file.transferTo(f);
			imagePath = f.getPath();
			result.setSuccess(true);
			result.setData(uploadDir + "/"+ f.getName());
		} catch (Exception e) {
			throw new WebException(HttpStatus.BAD_REQUEST, "图片上传失败");
		}
		return result;
	}
}
